<template>
  <div class="container">
    <div class="question-container">
      <el-card class="box-card">
        <el-breadcrumb
          separator-class="el-icon-arrow-right"
          v-if="detail.id && detail.name"
        >
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{
            detail.id ? detail.name : ""
          }}</el-breadcrumb-item>
          <el-breadcrumb-item>标签管理</el-breadcrumb-item>
        </el-breadcrumb>
        <el-divider v-if="detail.id && detail.name"></el-divider>
        <!-- 头部类容 -->
        <el-row>
          <el-col :span="18">
            <el-form label-width="80px">
              <el-form-item label="标签名称" style="display: inline-block">
                <el-input
                  v-model="obj.tagName"
                  style="width: 200px; height: 33px"
                >
                </el-input>
              </el-form-item>
              <el-form-item label="状态" style="display: inline-block">
                <el-select v-model="obj.state" placeholder="请选择" clearable>
                  <el-option label="启用" :value="1"></el-option>
                  <el-option label="禁用" :value="0"></el-option>
                </el-select>
                <span style="margin-left: 10px">
                  <el-button size="mini" @click="clear">清除</el-button>
                  <el-button type="primary" size="mini" @click="search"
                    >搜索</el-button
                  >
                </span>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="6" style="text-align: right">
                <el-button type="text" v-if="detail.id && detail.name" @click="$router.back()" class="el-icon-back">返回学科</el-button>
            <el-button
              class="el-icon-edit"
              type="success"
              size="small"
              style="text-align: right"
              @click="add"
              >新增标签</el-button
            >
          </el-col>
        </el-row>
        <el-alert type="info" show-icon :closable="false">
          <template title>
            <span>数据一共{{ this.counts }}条</span>
          </template>
        </el-alert>
        <!-- 头部类容 -->
        <!-- table列表 -->
        <el-table :data="tableData" style="width: 100%; margin: 15px">
          <el-table-column type="index" label="序号" width="50">
          </el-table-column>
          <el-table-column prop="subjectName" label="所属学科" width="210">
          </el-table-column>
          <el-table-column prop="tagName" label="标签名称"> </el-table-column>
          <el-table-column prop="username" label="创建者"> </el-table-column>
          <el-table-column label="创建日期">
            <template slot-scope="{ row }">
              {{ row.addDate | parseTimeByString }}
            </template>
          </el-table-column>
          <el-table-column prop="state" label="状态">
            <template slot-scope="{ row }">
              {{ !!row.state ? "已启用" : "已禁用" }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="240">
            <template slot-scope="{ row }">
              <el-button type="text" @click="forbidden(row)">
                <template>
                  {{ !row.state ? "启用" : "禁用" }}
                </template>
              </el-button>
              <el-button
                v-if="row.state === 1"
                type="text"
                disabled
                @click="edit(row)"
                >修改</el-button
              >
              <el-button v-else type="text" @click="edit(row)">修改</el-button>
              <el-button
                v-if="row.state === 1"
                type="text"
                disabled
                @click="Delete(row)"
                >删除</el-button
              >
              <el-button v-else type="text" @click="Delete(row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- table列表 -->
        <!-- 分页 -->
        <div class="block" style="text-align: right">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="obj.page"
            :page-sizes="[5, 10, 20, 50]"
            :page-size="obj.pagesize"
            :total="this.counts"
            background
            layout="total, sizes, prev, pager, next, jumper"
          >
          </el-pagination>
        </div>
        <!-- 分页 -->
        <tags-add
          v-model="openDialog"
          ref="tags"
          :select="selectTable"
          @add-succsee="loadtagsList"
        />
      </el-card>
    </div>
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/tags.js'
import { simple } from '@/api/hmmm/subjects.js'

import tagsAdd from '../components/tags-add.vue'
export default {
  components: { tagsAdd },
  data () {
    return {
      form: {},
      counts: 0,
      obj: {
        page: 1,
        pagesize: 10,
        tagName: '',
        state: null
      },
      // obj2: {
      //   tagName: '',
      //   state: '',
      //   page: 1,
      //   pagesize: 10
      // },
      tableData: [],
      selectTable: [],
      openDialog: false
    }
  },
  created () {
    this.loadtagsList()
    this.loadSimpel()
  },
  computed: {
    detail () {
      return this.$route.query || null
    }
  },
  watch: {
    '$route.query' () {
      this.loadtagsList()
    }
  },
  methods: {
    handleSizeChange (val) {
      this.obj.pagesize = val
      this.loadtagsList()
    },
    handleCurrentChange (val) {
      this.obj.page = val
      this.loadtagsList()
    },
    // 渲染列表
    async loadtagsList () {
      this.obj.subjectID = this.detail.id || null
      const res = await list(this.obj)
      this.counts = res.data.counts
      this.tableData = res.data.items
    },
    // 渲染简单列表
    async loadSimpel () {
      const res1 = await simple()
      this.selectTable = res1.data
      // console.log(res1)
    },
    // 点击搜索
    async search () {
      const res = await list(this.obj)
      this.tableData = res.data.items
    },
    // 点击清除
    clear () {
      this.obj.tagName = ''
      this.obj.state = null
      this.loadtagsList()
    },
    // 禁用或启用按钮
    async forbidden (row) {
      try {
        await changeState({
          id: row.id,
          state: row.state === 1 ? 0 : 1
        })
        row.state = row.state === 1 ? 0 : 1
      } catch (err) {
        console.log(err)
      }
      this.$message.success('操作成功')
    },
    add () {
      this.openDialog = true
    },
    // 点击修改
    edit (row) {
      // console.log(row)
      this.openDialog = true
      this.$refs.tags.getName(row)
    },
    // 点击删除
    Delete (row) {
      this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await remove(row)
          this.loadtagsList()
          this.$message.success('删除成功')
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style scoped lang="less">
.question-container {
  padding: 0 10px;
  margin: 10px 0;
  /deep/.el-alert__icon {
    font-size: 16px;
    width: 16px;
  }
}
</style>
